package xin.marcher.module.seller.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xin.marcher.module.common.constants.seller.SellerRedisKeyConstants;
import xin.marcher.module.common.constants.seller.SkuSellerRelationConstants;
import xin.marcher.module.common.enums.YesOrNoEnum;
import xin.marcher.module.common.enums.seller.NationalCodeLevelEnum;
import xin.marcher.module.common.enums.seller.SellerInfoStatusEnum;
import xin.marcher.module.common.enums.seller.SellerPositionEnum;
import xin.marcher.module.common.enums.seller.SellerTypeEnum;
import xin.marcher.module.common.exception.BaseBizException;
import xin.marcher.module.common.exception.ProductErrorCodeEnum;
import xin.marcher.module.common.utils.BopomofoUtil;
import xin.marcher.module.seller.cache.SellerInfoCache;
import xin.marcher.module.seller.domain.dto.SellerInfoResultDTO;
import xin.marcher.module.seller.domain.entity.NationalCodeDO;
import xin.marcher.module.seller.domain.entity.SellerInfoDO;
import xin.marcher.module.seller.domain.request.NationalCodeRequest;
import xin.marcher.module.seller.domain.request.SellerInfoRequest;
import xin.marcher.module.seller.domain.response.SellerInfoResponse;
import xin.marcher.module.seller.repository.NationalCodeRepository;
import xin.marcher.module.seller.repository.SellerRepository;
import xin.marcher.module.seller.service.SellerInfoService;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import static xin.marcher.module.common.constants.seller.SellerRedisKeyConstants.CHILD_SELLER_COLLECTION;

/**
 *
 */
@Service
public class SellerInfoServiceImpl implements SellerInfoService {

    /**
     * 卖家ID，6位序列号
     */
    private static final int SELLER_ID_WIDTH = 6;

    /**
     * 自营卖家编码，4位序列号
     */
    private static final int SELLER_CODE_WIDTH = 4;

    @Autowired
    private SellerRepository sellerRepository;

    @Autowired
    private SellerInfoCache sellerInfoCache;

    @Autowired
    private NationalCodeRepository nationalCodeRepository;

    /**
     * 新增卖家
     *
     * @param request
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public SellerInfoResultDTO saveSellerInfo(SellerInfoRequest request) {
        // 1. 校验参数完整性
        checkSaveSellerInfoRequest(request);
        // 2. 父卖家是否存在
        SellerInfoDO sellerInfo = checkParentSellerExists(request);
        // 3. sellerId生成
        Long sellerId = sellerRepository.generateSellerId(SkuSellerRelationConstants.SELLER_PREFIX_NO, SELLER_ID_WIDTH);
        request.setSellerId(sellerId);
        // 新增卖家的父卖家的sellerCode是国标，找到国标前缀
        String sellerCodePrefix = getSellerCodePrefix(sellerInfo);
        // 4. sellerCode生成
        String sellerCode = sellerRepository.generateSellerNo(sellerCodePrefix, SELLER_CODE_WIDTH);
        request.setSellerCode(sellerCode);
        // 5. 新增卖家数据
        sellerRepository.saveSellerInfo(request);
        return new SellerInfoResultDTO(request.getSellerId(), Boolean.TRUE);
    }

    /**
     * 获取自营卖家叶子结点的sellerCode前缀
     *
     * @param parentSellerInfo
     * @return
     */
    private String getSellerCodePrefix(SellerInfoDO parentSellerInfo) {
        // 父卖家编码
        Integer sellerCode;
        try {
            sellerCode = Integer.parseInt(parentSellerInfo.getSellerCode());
        } catch (NumberFormatException e) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }

        NationalCodeRequest request = new NationalCodeRequest();
        request.setId(sellerCode);
        List<NationalCodeDO> nationalCodeList = nationalCodeRepository.queryNationalCode(request);

        NationalCodeDO nationalCode = nationalCodeList.get(0);
        if (Objects.isNull(nationalCode) || StringUtils.isEmpty(nationalCode.getShortName())) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
        // 转换为拼音首字母简写
        return BopomofoUtil.initialsTransfer(nationalCode.getShortName()) + SkuSellerRelationConstants.SELF_SELLER_CODE_SEPARATOR;
    }

    /**
     * 更新卖家信息
     * <p>
     * 允许修改卖家名、卖家描述和卖家状态字段
     * 变更卖家状态后，所有子卖家状态也要变更，但仅允许修改城市及以下层级的卖家状态
     *
     * @param request
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public SellerInfoResultDTO updateSellerInfo(SellerInfoRequest request) {
        // 1. 校验参数完整性
        checkUpdateSellerInfoRequest(request);
        // 2. 修改卖家数据
        sellerRepository.updateSellerInfo(request);
        // sellerStatus是否变更
        if (Objects.nonNull(request.getSellerStatus())) {
            QueryChildSeller queryChildSeller = new QueryChildSeller(Collections.singletonList(request.getSellerId()), request.getSellerType());
            // 查询出所有的子卖家的sellerId
            List<Long> sellerIdList = getChildSellerIdList(queryChildSeller);
            // 批量修改状态
            sellerRepository.batchUpdateSellerInfo(sellerIdList, request.getSellerStatus());
        }
        return new SellerInfoResultDTO(request.getSellerId(), Boolean.TRUE);
    }

    /**
     * 获取子卖家的sellerId
     *
     * @param request
     * @return
     */
    @Override
    public List<Long> getChildSellerIdList(SellerInfoRequest request) {
        if (Objects.isNull(request)) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
        QueryChildSeller queryChildSeller = new QueryChildSeller(Collections.singletonList(request.getSellerId()), request.getSellerType());
        Optional<List<Long>> sellerIdListOps = sellerInfoCache.listRedisStringDataByCache(request.getSellerId(),
                Long.class,
                CHILD_SELLER_COLLECTION + request.getSellerId(),
                request,
                req -> Optional.of(getChildSellerIdListFromDB(queryChildSeller)));
        return sellerIdListOps.orElse(Collections.emptyList());
    }

    /**
     * 获取子卖家的sellerId
     *
     * @param queryChildSeller
     * @return
     */
    private List<Long> getChildSellerIdListFromDB(QueryChildSeller queryChildSeller) {
        if (Objects.isNull(queryChildSeller.getSellerType())) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }

        List<Long> sellerIdList = Lists.newArrayList();
        List<Long> sellerIds = sellerRepository.getSellerIdListByParentId(queryChildSeller.getParentIdList(), queryChildSeller.getSellerType());
        if (CollectionUtils.isNotEmpty(sellerIds)) {
            sellerIdList.addAll(sellerIds);
            queryChildSeller.setParentIdList(sellerIds);
            sellerIdList.addAll(getChildSellerIdListFromDB(queryChildSeller));
        }
        return sellerIdList;
    }


    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    private class QueryChildSeller extends SellerInfoRequest {
        /**
         * 父卖家ID集合
         */
        private List<Long> parentIdList;

        /**
         * 父卖家类型
         */
        private Integer sellerType;
    }

    /**
     * 查询卖家，用于运营人员前端界面筛选卖家操作
     *
     * @param request
     * @return
     */
    @Override
    public List<SellerInfoResponse> querySellerInfo(SellerInfoRequest request) {
        // 校验参数完整性
        checkQuerySellerInfoRequest(request);
        if (CollectionUtils.isEmpty(request.getSellerIdList())) {
            // 根据sellerType获取该类型下的sellerId集合
            Optional<List<Long>> sellerIdListOps = getSellerIdListBySellerType(request);
            sellerIdListOps.ifPresent(request::setSellerIdList);
        }

        // 根据sellerIdList从缓存或者数据库中查询卖家信息
        Optional<List<SellerInfoResponse>> sellerInfoListOps = sellerInfoCache.listRedisStringDataByCache(request.getSellerIdList(),
                SellerInfoResponse.class,
                sellerId -> SellerRedisKeyConstants.SELLER_INFO_LIST + sellerId,
                request,
                req -> sellerRepository.querySellerInfo(request));
        return sellerInfoListOps.orElse(Collections.emptyList());
    }

    /**
     * 不同系统间调用RPC接口，查询卖家
     * <p>
     * 卖家系统提供给外部系统调用的卖家查询接口，只允许通过sellerIdList或者sellerType两个参数进行查询
     *
     * @return
     */
    @Override
    public List<SellerInfoResponse> querySellerInfoForRPC(SellerInfoRequest request) {
        // 参数校验，RPC接口根据sellerIdList和sellerType查询
        checkQuerySellerInfoRequestByRPC(request);
        // 如果未传入sellerIdList
        if (CollectionUtils.isEmpty(request.getSellerIdList())) {
            // 根据sellerType获取该类型下的sellerId集合
            Optional<List<Long>> sellerIdListOps = getSellerIdListBySellerType(request);
            // 如果类型下没有sellerId数据，直接返回空卖家集合
            if (!sellerIdListOps.isPresent()) {
                return Collections.emptyList();
            }
            // 将根据sellerType查询到的sellerIdList数据set到request中
            request.setSellerIdList(sellerIdListOps.get());
        }

        // 根据sellerIdList从缓存或者数据库中查询卖家信息
        Optional<List<SellerInfoResponse>> sellerInfoListOps = sellerInfoCache.listRedisStringDataByCache(request.getSellerIdList(),
                SellerInfoResponse.class,
                sellerId -> SellerRedisKeyConstants.SELLER_INFO_LIST + sellerId,
                // 根据sellerId从DB中获取SellerInfo数据
                sellerId -> sellerRepository.querySellerInfoBySellerId(sellerId));
        if (!sellerInfoListOps.isPresent()) {
            return Collections.emptyList();
        }
        // 过滤类型不一致的卖家信息
        return filterAccordantSellerInfo(sellerInfoListOps.get(), request);
    }

    /**
     * 过滤掉卖家类型不符合参数条件的卖家信息
     *
     * @param sellerInfoList
     * @param request
     * @return
     */
    private List<SellerInfoResponse> filterAccordantSellerInfo(List<SellerInfoResponse> sellerInfoList, SellerInfoRequest request) {
        if (Objects.isNull(SellerTypeEnum.getByCode(request.getSellerType()))) {
            return sellerInfoList;
        }

        List<SellerInfoResponse> sellerInfoResponseList = sellerInfoList.stream().filter(sellerInfoResponse -> {
            if (sellerInfoResponse.getSellerType().equals(request.getSellerType())) {
                return true;
            }
            return false;
        }).collect(Collectors.toList());
        return sellerInfoResponseList;
    }

    /**
     * 获取某类型下的所有卖家的SellerId集合
     * <p>
     * 先从缓存中获取，如果缓存中没有，则加上分布式锁，从数据库中获取
     * 从数据库中获取到后，重新set到缓存，并释放分布式锁
     * 如果从数据库中也没有获取到，则设置一个空缓存，并指定随机的过期时间
     * <p>
     * 缓存类型为string -> type::[页数]:[sellerId1, sellerId2, ...]
     * 缓存结构如下例：
     * key -> type:1
     * value -> "[20480670, 20480798, 20481694]"
     * <p>
     * 分页存储是为了防止单个key过大
     *
     * @param request
     * @return
     */
    private Optional<List<Long>> getSellerIdListBySellerType(SellerInfoRequest request) {
        String redisKey = "";
        if (request.getSellerType().equals(SellerTypeEnum.SELF.getCode())) {
            redisKey = SellerRedisKeyConstants.SELF_TYPE_LIST + request.getPageNo();
        } else {
            redisKey = SellerRedisKeyConstants.POP_TYPE_LIST + request.getPageNo();
        }

        Optional<List<Long>> sellerIdListOps = sellerInfoCache.listRedisStringDataByCache(request.getPageNo().longValue(),
                Long.class,
                redisKey,
                request,
                req -> sellerRepository.pageSellerIdListByType(request));

        return sellerIdListOps;
    }

    /**
     * RPC接口查询卖家参数校验
     *
     * @param request
     */
    private void checkQuerySellerInfoRequestByRPC(SellerInfoRequest request) {
        checkQuerySellerInfoRequest(request);

        // 如果sellerIdList和sellerType都未传入
        if (CollectionUtils.isEmpty(request.getSellerIdList()) && Objects.isNull(SellerTypeEnum.getByCode(request.getSellerType()))) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
    }

    /**
     * 删除卖家
     *
     * @param sellerIdList
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public SellerInfoResultDTO deleteSellerInfo(List<Integer> sellerIdList) {
        // 1. 是否存在非叶子卖家
        if (sellerRepository.checkNonLastSellerExists(sellerIdList)) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
        // 2. 删除
        sellerRepository.deleteSellerInfo(sellerIdList);
        return new SellerInfoResultDTO(null, Boolean.TRUE);
    }

    /**
     * 卖家树信息初始化接口
     *
     * @param sellerType
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public SellerInfoResultDTO initSellerTreeInfo(Integer sellerType) {
        // 1. 生成全国卖家
        Long nationwideSellerId = initNationwideSeller(sellerType);
        // 2. 生成大区卖家
        Map<String, Long> districtMap = initDistrictSeller(nationwideSellerId, sellerType);
        // 3. 生成省/直辖市卖家
        Map<String, Long> provinceMap = initDiffParentSeller(districtMap, sellerType, NationalCodeLevelEnum.PROVINCE, SellerPositionEnum.PROVINCE);
        // 4. 生成城市卖家
        Map<String, Long> cityMap = initDiffParentSeller(provinceMap, sellerType, NationalCodeLevelEnum.CITY, SellerPositionEnum.CITY);
        // 5. 生成地区卖家
        initDiffParentSeller(cityMap, sellerType, NationalCodeLevelEnum.REGION, SellerPositionEnum.REGION);
        return new SellerInfoResultDTO(null, Boolean.TRUE);
    }

    /**
     * 省/直辖市、城市、区县卖家都使用该接口生成
     *
     * @param districtMap
     * @param sellerType
     * @param codeLevelEnum
     * @return
     */
    private Map<String, Long> initDiffParentSeller(Map<String, Long> districtMap,
                                                   Integer sellerType,
                                                   NationalCodeLevelEnum codeLevelEnum,
                                                   SellerPositionEnum sellerPositionEnum) {
        // 查询出地区层级数据
        List<NationalCodeDO> provinceCodeList = getNationalCode(codeLevelEnum);
        // 需要保存的卖家集合
        List<SellerInfoDO> sellerInfoList = provinceCodeList.stream().map(nationalCode -> {
            // 获取该国标的上级卖家ID
            Long parentId = districtMap.get(String.valueOf(nationalCode.getParentId()));
            return buildSellerInfoDO(sellerType, nationalCode, parentId, sellerPositionEnum);
        }).collect(Collectors.toList());
        // 批量保存
        sellerRepository.savelSellerInfoBatch(sellerInfoList);
        // 转换为Map，key为卖家编码、value为卖家ID
        return sellerInfoList.stream().collect(Collectors.toMap(SellerInfoDO::getSellerCode, SellerInfoDO::getSellerId));
    }

    /**
     * 生成地区卖家
     *
     * @param nationwideSellerId
     * @param sellerType
     */
    private Map<String, Long> initDistrictSeller(Long nationwideSellerId, Integer sellerType) {
        // 查询出地区层级数据
        List<NationalCodeDO> nationalCodeList = getNationalCode(NationalCodeLevelEnum.DISTRICT);
        // 国家以下层级有多个
        List<SellerInfoDO> sellerInfoList = nationalCodeList.stream()
                .map(nationalCode -> buildSellerInfoDO(sellerType, nationalCode, nationwideSellerId, SellerPositionEnum.DISTRICT))
                .collect(Collectors.toList());
        // 批量保存
        sellerRepository.savelSellerInfoBatch(sellerInfoList);
        // 转换为Map，key为卖家编码、value为卖家ID
        return sellerInfoList.stream().collect(Collectors.toMap(SellerInfoDO::getSellerCode, SellerInfoDO::getSellerId));
    }

    /**
     * 生成全国卖家
     *
     * @param sellerType 卖家类型
     * @return 全国卖家的sellerId
     */
    private Long initNationwideSeller(Integer sellerType) {
        // 查询出国家层级数据
        List<NationalCodeDO> nationalCodeList = getNationalCode(NationalCodeLevelEnum.NATIONWIDE);
        // 只有一个全国卖家
        NationalCodeDO nationalCode = nationalCodeList.get(0);

        // 生成sellerId
        Long sellerId = sellerRepository.generateSellerId(SkuSellerRelationConstants.SELLER_PREFIX_NO, SELLER_ID_WIDTH);
        SellerInfoDO sellerInfo = buildSellerInfoDO(sellerType, nationalCode, sellerId);
        // 保存全国卖家
        sellerRepository.savelSellerInfoBatch(Collections.singletonList(sellerInfo));
        return sellerId;
    }

    /**
     * 构造卖家树节点数据
     *
     * @param sellerType
     * @param nationalCode
     * @param sellerId
     * @return
     */
    private SellerInfoDO buildSellerInfoDO(Integer sellerType, NationalCodeDO nationalCode, Long sellerId) {
        // 构建全国卖家数据
        SellerInfoDO sellerInfo = new SellerInfoDO();
        sellerInfo.setSellerId(sellerId);
        sellerInfo.setSellerCode(String.valueOf(nationalCode.getId()));
        sellerInfo.setSellerName(nationalCode.getName() + SkuSellerRelationConstants.SELLER_NAME_SUFFIX);
        sellerInfo.setSellerPosition(SellerPositionEnum.NATIONWIDE.getCode());
        sellerInfo.setSellerType(sellerType);
        sellerInfo.setSellerStatus(SellerInfoStatusEnum.OPEN_STATUS.getCode());
        sellerInfo.setParentId(SkuSellerRelationConstants.ROOT_PARENT_ID);
        sellerInfo.setLastNode(YesOrNoEnum.NO.getCode());
        sellerInfo.initCommon();
        return sellerInfo;
    }

    /**
     * 构造卖家树节点数据
     *
     * @param sellerType
     * @param nationalCode
     * @param parentId
     * @param province
     * @return
     */
    private SellerInfoDO buildSellerInfoDO(Integer sellerType,
                                           NationalCodeDO nationalCode,
                                           Long parentId,
                                           SellerPositionEnum province) {
        SellerInfoDO sellerInfo = new SellerInfoDO();
        // 生成sellerId
        Long sellerId = sellerRepository.generateSellerId(SkuSellerRelationConstants.SELLER_PREFIX_NO, SELLER_ID_WIDTH);
        sellerInfo.setSellerId(sellerId);
        sellerInfo.setSellerCode(String.valueOf(nationalCode.getId()));
        sellerInfo.setSellerName(nationalCode.getName() + SkuSellerRelationConstants.SELLER_NAME_SUFFIX);
        sellerInfo.setSellerPosition(province.getCode());
        sellerInfo.setSellerType(sellerType);
        sellerInfo.setSellerStatus(SellerInfoStatusEnum.OPEN_STATUS.getCode());
        sellerInfo.setParentId(parentId);
        sellerInfo.setLastNode(YesOrNoEnum.NO.getCode());
        sellerInfo.initCommon();
        return sellerInfo;
    }

    /**
     * 查询某地区层级的国标数据
     *
     * @param province
     * @return
     */
    private List<NationalCodeDO> getNationalCode(NationalCodeLevelEnum province) {
        NationalCodeRequest nationalRequest = new NationalCodeRequest();
        nationalRequest.setLevelType(province.getCode());
        return nationalCodeRepository.queryNationalCode(nationalRequest);
    }

    /**
     * 查询卖家参数校验
     *
     * @param request
     */
    private void checkQuerySellerInfoRequest(SellerInfoRequest request) {
        if (Objects.isNull(request)) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
        // 分页大小超过最大限制
        if (request.getPageSize() > SkuSellerRelationConstants.QUERY_MAX_PAGE_SIZE) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
        // 卖家ID数量超过最大限制
        if (CollectionUtils.isNotEmpty(request.getSellerIdList()) &&
                CollectionUtils.size(request.getSellerIdList()) > SkuSellerRelationConstants.QUERY_SELLER_INFO_MAX_ID_NUMBER) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
    }

    /**
     * 检查parentId对应的父卖家是否存在，以及父卖家的层级是否是地区层级
     *
     * @param request
     * @return 父卖家
     */
    private SellerInfoDO checkParentSellerExists(SellerInfoRequest request) {
        // 父卖家ID
        Integer parentId = request.getParentId();
        if (Objects.isNull(parentId)) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }

        // 检查父卖家是否存在
        SellerInfoDO sellerInfo = sellerRepository.sellerParentExists(parentId);
        if (Objects.isNull(sellerInfo)) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }

        // 检查父卖家层级是否是地区
        if (!sellerInfo.getSellerPosition().equals(SellerPositionEnum.REGION.getCode())) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }

        return sellerInfo;
    }

    /**
     * 新增卖家操作入参检查
     *
     * @param request
     */
    private void checkSaveSellerInfoRequest(SellerInfoRequest request) {
        if (Objects.isNull(request)) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
        // 卖家名
        String sellerName = request.getSellerName();
        // 卖家位置
        Integer sellerPosition = request.getSellerPosition();
        // 是否是叶子结点
        Integer lastNode = request.getLastNode();
        // 父卖家ID
        Integer parentId = request.getParentId();
        // 卖家类型
        Integer sellerType = request.getSellerType();
        // 卖家状态
        Integer status = request.getSellerStatus();

        // 卖家名必须传入
        if (StringUtils.isEmpty(sellerName)) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
        // 卖家状态必传
        if (Objects.isNull(SellerInfoStatusEnum.getByCode(status))) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
        // 父卖家ID必传
        if (Objects.isNull(parentId) || parentId.equals(BigDecimal.ZERO.intValue())) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }

        // 位置、类型、是否是叶子结点，禁止传入，新增卖家的位置是微仓、类型是自营、必是叶子结点

        // 位置禁传
        if (Objects.nonNull(sellerPosition)) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
        // 卖家类型禁传
        if (Objects.nonNull(sellerType)) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
        // 是否是叶子禁传
        if (Objects.nonNull(lastNode)) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
    }

    /**
     * 修改卖家操作入参校验
     *
     * @param request
     */
    private void checkUpdateSellerInfoRequest(SellerInfoRequest request) {
        if (Objects.isNull(request)) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }

        /**
         * sellerId作为条件必须传入
         */
        if (Objects.isNull(request.getSellerId())) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
        // 允许修改的只有卖家名、卖家描述、卖家状态

        // 父卖家ID、卖家编码、卖家位置、叶子结点、卖家类型不允许修改
        if (Objects.nonNull(request.getParentId())) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
        if (StringUtils.isNotEmpty(request.getSellerCode())) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
        if (Objects.nonNull(request.getSellerPosition())) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
        if (Objects.nonNull(request.getLastNode())) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
    }
}
